/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlserver;

/**
 *
 * @author BEHIEN
 */
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;


public class MetaManipulation {

	public static final int ErrorUnknown = -1;
	public static final int ErrorLoading = -2;
	public static final int ErrorExisted = -3;
	public static final int ErrorInvalidAgrument = -4;
	public static final int ErrorDBNotFound = -5;
	public static final int ErrorTableNotFound = -6;
	public static final int ErrorColumnNotFound = -7;
	public static final int ErrorConstraintNotFound = -8;
	public static final int ErrorConstraintReferenceNotFound = -9;
	public static final int ErrorInvalidTypeOnColumnReference = -10;
	public static final int Success = 1;
	
	
	public static void init_doc(String dbName, String tableName)
	{
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder;
		try {
			docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("tables");
			
			/*
			Element tb = doc.createElement("Table");
			rootElement.appendChild(tb);
			
			Attr attr = doc.createAttribute("Name");
			attr.setValue(tableName);
			tb.setAttributeNode(attr);
			*/
			
			doc.appendChild(rootElement);
			save_doc(doc, dbName);
		} catch (ParserConfigurationException e) {
		}

	}
	
	
	public static int save_doc(Document doc, String dbName) {
		String fileName = dbName + ".xml";
		File file = new File(fileName);
		
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = null;
		try {
			transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(file);
			try {
				transformer.transform(source, result);
				return Success;
			} catch (TransformerException e) {
				return ErrorUnknown;
			}
		} catch (TransformerException  e) {
			return ErrorUnknown;
		}

	}
	
	
	public static Document load_doc(String dbName, String tableName) {
		
		String META_Path = dbName + ".xml";

		File file = new File(META_Path);
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		Document doc = null;
		try {
			db = dbf.newDocumentBuilder();
			doc = db.parse(file);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// error file meta not found. Create new file meta
			init_doc(dbName, tableName);
			try {
				db = dbf.newDocumentBuilder();
				doc = db.parse(file);
			} catch (ParserConfigurationException e1) {
				e1.printStackTrace();
			} catch (SAXException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return doc;

	}
	
	
	public static boolean CheckNodeExists(String dbName, String tableName) {
		try {
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        Document document = dbf.newDocumentBuilder().parse(new File(dbName + ".xml"));

			XPath xpath = XPathFactory.newInstance().newXPath();
			// XPath Query for showing all nodes value
			XPathExpression expr = xpath.compile("//table[@name='" + tableName
					+ "']");
			Node resultNode = (Node) expr.evaluate(document, XPathConstants.NODE);
			if (resultNode != null) {
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}
        
        public static void init_select()
	{
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder;
		try {
			docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("statement");
			
			/*
			Element tb = doc.createElement("Table");
			rootElement.appendChild(tb);
			
			Attr attr = doc.createAttribute("Name");
			attr.setValue(tableName);
			tb.setAttributeNode(attr);
			*/
			
			doc.appendChild(rootElement);
			save_select(doc);
		} catch (ParserConfigurationException e) {
		}

	}
	
	
	public static int save_select(Document doc) {
		String fileName = "select.xml";
		File file = new File(fileName);
		
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = null;
		try {
			transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(file);
			try {
				transformer.transform(source, result);
				return Success;
			} catch (TransformerException e) {
				return ErrorUnknown;
			}
		} catch (TransformerException  e) {
			return ErrorUnknown;
		}

	}
	
	
	public static Document load_select() {
		
		String META_Path = "select.xml";

		File file = new File(META_Path);
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		Document doc = null;
		try {
			db = dbf.newDocumentBuilder();
			doc = db.parse(file);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// error file meta not found. Create new file meta
			init_select();
			try {
				db = dbf.newDocumentBuilder();
				doc = db.parse(file);
			} catch (ParserConfigurationException e1) {
				e1.printStackTrace();
			} catch (SAXException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return doc;

	}

}